package com.yugao.fintech.antelope.gateway.listener;

import com.rabbitmq.client.Channel;
import com.yugao.fintech.antelope.gateway.service.AuthService;
import com.yugao.fintech.antelope.base.model.module.mq.OAuth2ClientMQ;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * 客户端刷新监听
 */
@Slf4j
@Component
@RabbitListener(bindings = {
        @QueueBinding(
                // 切记: 此处无需设置队列名称，否在得话，多个消费者只有一个消费者能消费数据。其它消费者无法消费数据
                value = @Queue(durable = "true"),
                exchange = @Exchange(value = OAuth2ClientMQ.EXCHANGE, type = ExchangeTypes.FANOUT))
})
public class OAuth2ClientRefreshListener {
    @Autowired
    private AuthService authService;

    @RabbitHandler
    public void onMessage(@Payload String msg, Channel channel, Message message) {
        log.info("删除本地 client 权限缓存, msg: {}", msg);
        OAuth2ClientMQ.MsgPayload payload = OAuth2ClientMQ.parse(msg, OAuth2ClientMQ.MsgPayload.class);
        authService.deleteClientAuthority(payload.getClientId());
    }
}
